﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using Fiddler;
using FiddlerEx_Inspector;
using FiddlerEx_Inspector.Properties;

namespace TextBoxEx
{
    public partial class TextBoxEx : TextBox
    {
        private CommonMethods cf = new CommonMethods();
        public TextBoxEx()
        {
            InitializeComponent();
        }

        public TextBoxEx(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        private void PB_JSONParse_Click(object sender, EventArgs e)
        {
            PB_DecryptOrEncrypt.Enabled = false;
            Text = CommonMethods.ConvertJsonString(Text.Trim());
            PB_DecryptOrEncrypt.Enabled = true;
        }

        private void PB_DecryptOrEncrypt_Click(object sender, EventArgs e)
        {
            PB_DecryptOrEncrypt.Enabled = false;
            if ((string)PB_DecryptOrEncrypt.Tag == "Decrypt")
            {
                Dictionary<string, string> encData = CommonMethods.GetEncResponse(null, Text);
                if (encData.Count > 0)
                {
                    Text = encData["encData"];
                    PB_DecryptOrEncrypt.Tag = "Encrypt";
                    PB_DecryptOrEncrypt.Image = Resources.Encrypt;
                    toolTip1.SetToolTip(PB_DecryptOrEncrypt, "密文");
                }
            }
            else if ((string)PB_DecryptOrEncrypt.Tag == "Encrypt")
            {
                Text = CommonMethods.GetDecResponse(null, Text);
                PB_DecryptOrEncrypt.Tag = "Decrypt";
                PB_DecryptOrEncrypt.Image = Resources.Decrypt;
                toolTip1.SetToolTip(PB_DecryptOrEncrypt, "明文");
            }
            PB_DecryptOrEncrypt.Enabled = true;
        }

        private void PB_CopyBody_Click(object sender, EventArgs e)
        {
            Utilities.CopyToClipboard(Text);
        }

        private void PB_JSONParse_MouseEnter(object sender, EventArgs e)
        {
            PB_JSONParse.BackColor = SystemColors.ControlDark;
        }

        private void PB_JSONParse_MouseLeave(object sender, EventArgs e)
        {
            PB_JSONParse.BackColor = Color.Transparent;
        }

        private void PB_DecryptOrEncrypt_MouseEnter(object sender, EventArgs e)
        {
            PB_DecryptOrEncrypt.BackColor = SystemColors.ControlDark;
        }

        private void PB_DecryptOrEncrypt_MouseLeave(object sender, EventArgs e)
        {
            PB_DecryptOrEncrypt.BackColor = Color.Transparent;
        }

        private void PB_CopyBody_MouseEnter(object sender, EventArgs e)
        {
            PB_CopyBody.BackColor = SystemColors.ControlDark;
        }

        private void PB_CopyBody_MouseLeave(object sender, EventArgs e)
        {
            PB_CopyBody.BackColor = Color.Transparent;
        }

        private void TextBoxE_TextChanged(object sender, EventArgs e)
        {
            if (CommonMethods.IsJson(Text.Trim()))
            {
                PB_DecryptOrEncrypt.Tag = "Decrypt";
                PB_DecryptOrEncrypt.Image = Resources.Decrypt;
                toolTip1.SetToolTip(PB_DecryptOrEncrypt, "明文");
            }
            else
            {
                PB_DecryptOrEncrypt.Tag = "Encrypt";
                PB_DecryptOrEncrypt.Image = Resources.Encrypt;
                toolTip1.SetToolTip(PB_DecryptOrEncrypt, "密文");
            }
        }
    }
}
